#!/usr/bin/sh
#THIS SCRIPT USED FOR CRAWLING SYSTEM INFO. FROM ALL PRODUCTION SERVERS.
#YOU ARE NOT AUTHORIZED TO MAKE ANY CHANGE UNLESS YOU ARE ONE OF THE SYSTEM ADMINS.
#AUTHOR:BAOXIWU<baoxiwu@boe.com.cn>, C&M, CIM, BOE-DT, BEIJING

#CONST DEFINITIONS
VERSION=0.9.0
USERHOME=/home/bao
OUTFILESUFFIX=`date +'-%y%m%d-%H%M.log'` 
OUTFILE=$USERHOME'/sysinfocrawler'$OUTFILESUFFIX
CFGFILE=sic.cfg
USER=root


echo "#SysInfoCrawler v"$VERSION>>$OUTFILE
echo "#All rights reserved by CIM, BOE-DT">>$OUTFILE
echo "#Any problem or BUG, plz. contact the author via email baoxiwu<baoxiwu@boe.com.cn>">>$OUTFILE



exec 3<$CFGFILE
while read line <&3; 
do
	#echo $line
	host=`echo $line | awk -F":" '{print $1}'`	
	cmds=`echo $line | awk -F":" '{print $2}'`
	#echo $host
	#echo $cmds
	
	cmd_lst=$cmds	

	#echo $cmd_lst
	SAVEDIFS="$IFS"
	IFS=","
	for cmd in $cmd_lst
	do
		echo $USER"@"$host"["$cmd"]..."
		echo $USER"@"$host"["$cmd"]">>$OUTFILE

		FLAG=0
		#handle bdf command
		indx=`echo $cmd|awk '{print index($0, "bdf")}'`
		if [ "$indx" -gt 0 ];then
			echo `remsh $host $cmd|awk '{if (index($5,"used") == 0){if (substr($5, 0, length($5) - 1) + 0 > '90') print $0}else{print $0}}'`>>$OUTFILE
			FLAG=1
		fi	

		#handle cat top.tmp
		indx=`echo $cmd|awk '{print index($0, "cat top.tmp")}'`
		if [ "$indx" -gt 0 ]; then
			echo `remsh $host $cmd|awk '{if (index($0,"System") > 0){print $0}}{if (index($0,"Load") > 0){print $0}}{if (index($0,"Memory") > 0){print $0}}'`>>$OUTFILE
			FLAG=1
		fi

		if [ "$FLAG" -eq 0 ];then
			echo `remsh $host $cmd`>>$OUTFILE
		fi

		echo "">>$OUTFILE
	done
	IFS="$SAVEDIFS"
done


